/*
 * #%L
 * SwiftUICommonUtilities
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.spring.jdbc;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This class Holds Metadata of Query Fired and also Result returned by the
 * query
 * 
 * @author ashwin
 * 
 */
public class DatabaseResult {
	private DBEntityComponent metadata = null;
	private List<List<MapObject>> data = null;
	private long totalRows = -1;
	private Integer offset = 0;
	private Map<String, String> columnRules = new HashMap<String, String>();
	private String presentDate = null;
	private Map rowValueMap = new HashMap();

	/**
	 * @return the presentDate
	 */
	public String getPresentDate() {
		return presentDate;
	}

	/**
	 * @param presentDate
	 *            the presentDate to set
	 */
	public void setPresentDate(String presentDate) {
		this.presentDate = presentDate;
	}

	public DatabaseResult() {
		presentDate = getFormattedDate("yyyy-MM-dd", new Date());
		// presentDate = getFormattedDate("MM/dd/yyyy", new Date());
		System.out.println("Present Date : " + presentDate);
	}

	/**
	 * @return the metadata
	 */
	public DBEntityComponent getMetadata() {
		return metadata;
	}

	/**
	 * @param metadata
	 *            the metadata to set
	 */
	public void setMetadata(DBEntityComponent metadata) {
		this.metadata = metadata;
	}

	/**
	 * @return the data
	 */
	public List<List<MapObject>> getData() {
		return data;
	}

	/**
	 * @param data
	 *            the data to set
	 */
	public void setData(List<List<MapObject>> data) {
		this.data = data;
	}

	/**
	 * @return the totalRows
	 */
	public long getTotalRows() {
		return totalRows;
	}

	/**
	 * @param totalRows
	 *            the totalRows to set
	 */
	public void setTotalRows(long totalRows) {
		this.totalRows = totalRows;
	}

	/**
	 * @return the offset
	 */
	public Integer getOffset() {
		return offset;
	}

	/**
	 * @param offset
	 *            the offset to set
	 */
	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	public Map<String, String> getColumnRules() {
		return columnRules;
	}

	public void setColumnRules(Map<String, String> columnRules) {
		this.columnRules = columnRules;
	}

	public List<String> getRuleForColumn(String columnName) {
		List<String> ruleList = new ArrayList<String>();
		try {
			if (columnRules != null) {
				String rulesString = columnRules.get(columnName);
				String[] rules = rulesString.split(";");
				for (String ruleString : rules) {
					ruleList.add(ruleString);
				}
			}
		} catch (Exception e) {
			return ruleList;
		}
		return ruleList;
	}

	public static String getFormattedDate(String format, Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String newDateString = formatter.format(date);
		return newDateString;
	}

	public static void main(String[] args) {
		System.out.println(getFormattedDate("MM/dd/yyyy", new Date()));
	}

	/**
	 * @return the rowValueMap
	 */
	public Map getRowValueMap() {
		return rowValueMap;
	}

	/**
	 * @param rowValueMap
	 *            the rowValueMap to set
	 */
	public void setRowValueMap(Map rowValueMap) {
		this.rowValueMap = rowValueMap;
	}

	public void putRowValue(Object key, Object value) {
		rowValueMap.put(key, value);
	}

}
